import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class AppRefreshHeader extends StatefulWidget {
  final Color? color;

  final ImageProvider? backgroundImage;

  final LinearGradient? backgroundGradient;

  final Color? backgroundColor;

  final double paddingTop;

  final double paddingBottom;

  AppRefreshHeader({
    this.color,
    this.backgroundImage,
    this.backgroundGradient,
    this.backgroundColor,
    this.paddingTop = 0.0,
    this.paddingBottom = 0.0,
  });

  @override
  _AppRefreshHeaderState createState() => _AppRefreshHeaderState();
}

class _AppRefreshHeaderState extends State<AppRefreshHeader> {
  late double appHeaderTriggerHeight;

  late double appRefreshHeaderHeight;

  late Color color;

  late bool isOuter;

  @override
  void initState() {
    if (widget.backgroundImage != null ||
        widget.backgroundColor != null ||
        widget.backgroundGradient != null) {
      this.isOuter = true;
      this.appHeaderTriggerHeight = 80.0;
      this.appRefreshHeaderHeight = 180.0;
      this.color = widget.color ?? Colors.white;
    } else {
      this.isOuter = false;
      this.appHeaderTriggerHeight = 35.0;
      this.appRefreshHeaderHeight = 70.0;
      this.color = widget.color ?? Colors.grey;
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return ClassicHeader(
      height: appHeaderTriggerHeight + widget.paddingTop + widget.paddingBottom,
      idleText: '下拉刷新',
      releaseText: '松开开始刷新',
      refreshingText: '刷新中',
      completeText: '刷新成功',
      textStyle: TextStyle(color: color),
      outerBuilder: (child) => isOuter
          ? AppRefreshHeaderOuter(
              child: child,
              appRefreshHeaderHeight: appRefreshHeaderHeight,
              appHeaderTriggerHeight: appHeaderTriggerHeight,
              backgroundImage: widget.backgroundImage,
              backgroundGradient: widget.backgroundGradient,
              backgroundColor: widget.backgroundColor,
            )
          : Container(
              padding: EdgeInsets.only(
                top: widget.paddingTop,
                bottom: widget.paddingBottom,
              ),
              height: appHeaderTriggerHeight +
                  widget.paddingTop +
                  widget.paddingBottom,
              alignment: Alignment.center,
              child: child,
            ),
      idleIcon: Icon(
        Icons.arrow_downward,
        color: color,
      ),
      releaseIcon: Icon(
        Icons.refresh,
        color: color,
      ),
      refreshingIcon: AppRefreshHeaderRefreshing(
        color: color,
      ),
      completeIcon: Icon(
        Icons.done,
        color: color,
      ),
    );
  }
}

// 刷新中的图标
class AppRefreshHeaderRefreshing extends StatelessWidget {
  final Color color;

  AppRefreshHeaderRefreshing({
    required this.color,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 20,
      height: 20,
      child: CircularProgressIndicator(
        valueColor: AlwaysStoppedAnimation<Color>(color),
        strokeWidth: 2,
      ),
    );
  }
}

// 背景
class AppRefreshHeaderOuter extends StatelessWidget {
  final Widget child;

  final double appRefreshHeaderHeight;

  final double appHeaderTriggerHeight;

  final ImageProvider? backgroundImage;

  final LinearGradient? backgroundGradient;

  final Color? backgroundColor;

  AppRefreshHeaderOuter({
    required this.child,
    required this.appRefreshHeaderHeight,
    required this.appHeaderTriggerHeight,
    this.backgroundImage,
    this.backgroundGradient,
    this.backgroundColor,
  });

  @override
  Widget build(BuildContext context) {
    BoxDecoration? boxDecoration;

    if (this.backgroundImage != null) {
      boxDecoration = BoxDecoration(
        image: DecorationImage(
          image: backgroundImage!,
          fit: BoxFit.fitWidth,
          alignment: Alignment.bottomCenter,
        ),
      );
    } else if (backgroundGradient != null) {
      boxDecoration = BoxDecoration(
        gradient: backgroundGradient,
      );
    } else if (backgroundColor != null) {
      boxDecoration = BoxDecoration(
        color: backgroundColor,
      );
    }

    return Container(
      height: appRefreshHeaderHeight +
          appHeaderTriggerHeight +
          MediaQuery.of(context).padding.top +
          kToolbarHeight,
      decoration: boxDecoration ?? BoxDecoration(),
      child: Stack(
        alignment: Alignment.bottomCenter,
        children: <Widget>[
          Align(
            alignment: Alignment(0, 0.9),
            child: child,
          ),
        ],
      ),
    );
  }
}
